package com.project.controller;

import com.github.pagehelper.PageInfo;
import com.project.bean.PostBean;
import com.project.service.IPostService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@RestController
@RequestMapping("post")
public class PostController {
    @Autowired
    private IPostService2 service;
    @RequestMapping("findByItem")
    public PageInfo<PostBean> findByItem(int id, int pageNnm, String title, String acc, LocalDateTime start, LocalDateTime end, HttpServletRequest request){
        request.getSession().setAttribute("typeId",id);
        return service.findByItem(id, pageNnm, title, acc, start, end);
    }

    @RequestMapping("addPost")
    public String addPost(String title,String content,int typeId,int userId){
        PostBean post = new PostBean(title,content,typeId,userId);
        service.add(post);
        return "ok";
    }

    @RequestMapping("findTypeId")
    public int type(HttpServletRequest request){
        Integer id = (Integer) request.getSession().getAttribute("typeId");
        return id;

    }

    @RequestMapping("findById")
    public PostBean findById(int id,HttpServletRequest request){

        PostBean postBean = service.findById(id);
        request.getSession().setAttribute("postBean",postBean);
        return postBean;
    }

}
